package com.macken.zjh;

import java.io.IOException;
import java.util.Random;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Screen;
import com.game.BaseResponse;
import com.game.IoHandlerAcceptor;
import com.game.SocketAcceptor;
import com.macken.zjh.constant.Constants;
import com.macken.zjh.entity.Desktop;
import com.macken.zjh.network.ResponseFactory;
import com.macken.zjh.network.res.ActionResponse;
import com.macken.zjh.network.res.DeskResponse;
import com.macken.zjh.network.res.SendResponse;
import com.macken.zjh.screens.EnterDeskLoad;
import com.macken.zjh.screens.GameScreen;
import com.macken.zjh.screens.MenuScreen;

public class Zjh extends Game {

	public static final int WIDTH = 800;
	public static final int HEIGHT = 480;

	public SocketAcceptor socketAcceptor;

	// private static final String host = "192.168.1.89";
	private static final String host = "10.1.161.42";
	private static final int port = 9999;

	public static final int pid = new Random().nextInt(1000)+1;

	@Override
	public void create() {
		socketAcceptor = new SocketAcceptor();
		socketAcceptor.addSocketStatusListener(new IoHandlerAcceptor() {

			@Override
			public void sessionClosed() {
				System.out.println("handler");
			}

			@Override
			public void messageReceived(BaseResponse resp) {
				handle(resp);
			}

			@Override
			public void exceptionCaught(IOException arg0) {
				System.out.println("exception");
			}
		});
		socketAcceptor.addResponseParser(new ResponseFactory());
		try {
			socketAcceptor.connet(host, port);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		setScreen(new MenuScreen(this));

	}

	public void handle(BaseResponse resp) {

		if (resp instanceof DeskResponse) {
			// 进入、退出、换桌
			DeskResponse response = (DeskResponse) resp;
			int did = response.did;
			byte state = response.state;
			int count = response.count;
			int[] pids = response.pids;
			byte[] states = response.states;
			byte[] positions = response.positions;

			Screen s = this.getScreen();
			if (s instanceof EnterDeskLoad) {
				EnterDeskLoad ls = (EnterDeskLoad) s;
				Desktop desk = new Desktop(did, state, count, pids, states,
						positions);
				ls.desk = desk;
				ls.state = 1;
			} else if (s instanceof GameScreen) {
				GameScreen gs = (GameScreen) s;
				gs.desktop.update(did, state, count, pids, states, positions);
				gs.renderDesk();
			}
		} else if (resp instanceof ActionResponse) {

			Screen s = this.getScreen();
			GameScreen gs = (GameScreen) s;
			ActionResponse response = (ActionResponse) resp;
			switch (response.action) {
			case Constants.ACTION_PREPARE:
				gs.desktop.playerMap.get(response.pid).state = Constants.PLAYER_STATE_READY;
				gs.renderDesk();
				break;
			default:
				break;
			}
		} else if (resp instanceof SendResponse) {
			Screen s = this.getScreen();
			if (s instanceof GameScreen) {
				GameScreen gs = (GameScreen) s;
				SendResponse sr = (SendResponse) resp;
				gs.desktop.state=Constants.DESK_STATE_SEND;
				gs.renderDesk();
				gs.sendCardAnimations(sr.pids);
				
			}
		}

	}
}
